// Created by Roman Novitsky on 04.06.2009


#region Imported Namespaces


using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;


#endregion


namespace WtfManager.Utilities
{
  public static class IconExtensions
  {
    #region Methods


    public static ImageSource ToImageSource(this Icon icon)
    {
      ImageSource source;
      using (var memStream = new MemoryStream())
      {
        Bitmap iconBitmap = icon.ToBitmap();
        iconBitmap.Save(memStream, ImageFormat.Png);
        memStream.Seek(0, SeekOrigin.Begin);
        var bitmapDecoder = new PngBitmapDecoder(memStream, BitmapCreateOptions.PreservePixelFormat,
                                                 BitmapCacheOption.OnLoad);
        source = bitmapDecoder.Frames[0];
        source.Freeze();
      }
      return source;
    }


    public static ImageSource ToImageSource(this Bitmap bitmap)
    {
      ImageSource source;
      using (var memStream = new MemoryStream())
      {
        bitmap.Save(memStream, ImageFormat.Bmp);
        memStream.Seek(0, SeekOrigin.Begin);
        var bitmapDecoder = new BmpBitmapDecoder(memStream, BitmapCreateOptions.PreservePixelFormat,
                                                 BitmapCacheOption.OnLoad);
        source = bitmapDecoder.Frames[0];
        source.Freeze();
      }
      return source;
    }


    #endregion
  }
}